package com.jie.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.reggie.commen.CustomException;
import com.jie.reggie.commen.R;
import com.jie.reggie.dto.DishDto;
import com.jie.reggie.entity.Category;
import com.jie.reggie.entity.Dish;
import com.jie.reggie.entity.DishFlavor;
import com.jie.reggie.service.CategoryService;
import com.jie.reggie.service.DishFlavorService;
import com.jie.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 曾豪杰
 * @Date: 2022/09/02/18:33
 * @Description:
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构造分页
        Page<Dish> pageInfo =new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage =new Page<>();
        //条件
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        //查询条件
        queryWrapper.like(name!=null,Dish::getName,name);
        //排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        //查询
        dishService.page(pageInfo,queryWrapper);

        //拷贝
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        List<Dish> records = pageInfo.getRecords();
        List<DishDto> list=records.stream().map((item)->{
            DishDto dishDto=new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();//分类id
            //获取分类通过分类id
            Category category = categoryService.getById(categoryId);

            String categoryName = category.getName();

            if(categoryName!=null){
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(list);

        return R.success(dishDtoPage);
    }

    /**
    * @Description: 回显
    * @Param: [id]
    * @return: com.jie.reggie.commen.R<com.jie.reggie.dto.DishDto>
    * @Author: 曾豪杰
    * @Date: 2022/9/3
    */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto byIdWithFlavor = dishService.getByIdWithFlavor(id);

        return R.success(byIdWithFlavor);
    }
    /**
    * @Description: 修改
    * @Param: [dishDto]
    * @return: com.jie.reggie.commen.R<java.lang.String>
    * @Author: 曾豪杰
    * @Date: 2022/9/3
    */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.updateWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }
    /**
    * @Description: 删除
    * @Param: [id]
    * @return: com.jie.reggie.commen.R<java.lang.String>
    * @Author: 曾豪杰
    * @Date: 2022/9/3
    */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        int count = dishService.count(queryWrapper);
        if(count>0){
            throw new CustomException("正在销售，不能删除");
        }
        dishService.removeByIds(ids);
        return R.success("删除成功");
    }   
    /**
    * @Description: 批量
    * @Param: [status, ids]
    * @return: com.jie.reggie.commen.R<java.lang.String>
    * @Author: 曾豪杰
    * @Date: 2022/9/3
    */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        List<Dish> list = dishService.list(queryWrapper);
        for (Dish dish : list) {
            dish.setStatus(status);
            dishService.updateById(dish);
        }
        return R.success("批量处理成功");
    }
    /**
    * @Description: 套餐添加查询菜品
    * @Param: [dish]
    * @return: com.jie.reggie.commen.R<java.util.List<com.jie.reggie.entity.Dish>>
    * @Author: 曾豪杰
    * @Date: 2022/9/3
    */
//    @GetMapping("/list")
//    public R<List<Dish>> list(Dish dish){
//        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
//        //查询
//        queryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
//        //排序条件
//        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
//        //状态条件
//        queryWrapper.eq(Dish::getStatus,1);
//
//        List<Dish> list = dishService.list(queryWrapper);
//        return R.success(list);
//    }
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        //查询
        queryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
        //排序条件
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        //状态条件
        queryWrapper.eq(Dish::getStatus,1);

        List<Dish> list = dishService.list(queryWrapper);

        List<DishDto> dtoList=list.stream().map((item)->{
            DishDto dishDto=new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();//分类id
            //获取分类通过分类id
            Category category = categoryService.getById(categoryId);

            String categoryName = category.getName();

            if(categoryName!=null){
                dishDto.setCategoryName(categoryName);
            }
            //口味
            Long dishId = item.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
            List<DishFlavor> list1 = dishFlavorService.list(lambdaQueryWrapper);
            dishDto.setFlavors(list1);
            return dishDto;
        }).collect(Collectors.toList());


        return R.success(dtoList);
    }
}
